<template>
  <el-menu :default-active="activeIndex" class="el-menu-vertical-demo" :defaultOpeneds="defaultOpeneds">
    <template v-for="subItem in menuList" :key="subItem.path">
      <el-sub-menu v-if="subItem.children?.length" :index="subItem.path">
        <template #title>
          <el-icon v-if="subItem.meta.icon">
            <component :is="subItem.meta.icon"></component>
          </el-icon>
          <span class="sle">{{ subItem.meta.title }}</span>
        </template>
        <SubMenu :menu-list="subItem.children" :secondLevelMenuKey="secondLevelMenuKey"
          :default-active="thirdLevelMenuKey" />
      </el-sub-menu>
      <el-menu-item v-else :index="subItem.path" @click="handleClickMenu(subItem)">
        <el-icon v-if="subItem.meta.icon">
          <component :is="subItem.meta.icon"></component>
        </el-icon>
        <template #title>
          <span class="sle">{{ subItem.meta.title }}</span>
        </template>
      </el-menu-item>
    </template>
  </el-menu>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from 'vue';
import { useRouter } from 'vue-router';

const props = defineProps<{ menuList: Menu.MenuOptions[], secondLevelMenuKey?: string, thirdLevelMenuKey?: string }>();

const router = useRouter();
const activeIndex = ref<any>('');

const handleClickMenu = (subItem: Menu.MenuOptions) => {
  if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");
  router.push(subItem.path);
};

watch(() => props.secondLevelMenuKey, (newKey) => {
  if (newKey) {
    activeIndex.value = newKey;
  }
}, { immediate: true });

// watch(() => props.menuList, (newMenuList) => {
//   if (newMenuList.length > 0 && !newMenuList[0].children) {
//     activeIndex.value = newMenuList[0].path;
//   } else if (newMenuList.length > 0 && newMenuList[0].children && newMenuList[0].children.length > 0) {
//     activeIndex.value = newMenuList[0].children[0].path;
//   }
// }, { immediate: true });

const defaultOpeneds = ref<string[]>([]);
onMounted(() => {
  if (props.menuList.length > 0) {
    defaultOpeneds.value = [props.menuList[0].path];
  }
});

watch(() => props.menuList, (newMenuList) => {
  if (newMenuList.length > 0) {
    defaultOpeneds.value = [newMenuList[0].path];
  }
}, { immediate: true });

</script>

<style lang="scss">
.el-sub-menu .el-sub-menu__title:hover {
  color: var(--el-menu-hover-text-color) !important;
  background-color: transparent !important;
}

.el-menu--collapse {
  .is-active {
    .el-sub-menu__title {
      color: #ffffff !important;
      background-color: var(--el-color-primary) !important;
    }
  }
}

.el-menu-item {
  &:hover {
    color: var(--el-menu-hover-text-color);
  }

  &.is-active {
    color: var(--el-menu-active-color) !important;
    background-color: var(--el-menu-active-bg-color) !important;

    &::before {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 4px;
      content: "";
      background-color: var(--el-color-primary);
    }
  }
}

.vertical,
.classic,
.transverse {
  .el-menu-item {
    &.is-active {
      &::before {
        left: 0;
      }
    }
  }
}

.columns {
  .el-menu-item {
    &.is-active {
      &::before {
        right: 0;
      }
    }
  }
}
</style>
